package cz.service;


import com.alibaba.fastjson.JSONObject;
import cz.dto.AccessToken;
import cz.dto.message.response.TextMessage;
import cz.util.net.HttpUtil;
import cz.util.wx.MessageUtil;
import cz.util.wx.WeixinUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.Date;
import java.util.Map;

/**
 * 网关业务层
 * @author 51452
 *
 */
public class GatewayService {
	
	
	/** 
     * 处理微信发来的请求 
     *  
     * @param request 
     * @return 
     */  
    public static String processRequest(HttpServletRequest request) {
        String respMessage = null;  
        try {  
            // 默认返回的文本消息内容  
            String respContent = "请求处理异常，请稍候尝试！";
  
            // xml请求解析  
            Map<String, String> requestMap = MessageUtil.parseXml(request);
  
            // 发送方帐号（open_id）  
            String fromUserName = requestMap.get("FromUserName");  
            // 公众帐号  
            String toUserName = requestMap.get("ToUserName");  
            // 消息类型  
            String msgType = requestMap.get("MsgType");
            //程震测试账号
//            AccessToken token =  WeixinUtil.getAccessToken("wx74f0b4d030cdae94","45c079284355ebf69af58e5eee18cd25");
            //韩浩测试账号
//            AccessToken token =  WeixinUtil.getAccessToken("wx0910213a09359dd4","ae54f06542d72ebf1e49b69d7f939271");
            //孙吴城管
            AccessToken token =  WeixinUtil.getAccessToken("wx9893470477aed97e","fa230874cf470202dfd5c889c32de06f");
            JSONObject jsonObject = HttpUtil.httpsRequest("https://api.weixin.qq.com/cgi-bin/user/info?access_token="+token.getToken()+"&openid=wx74f0b4d030cdae94&lang=zh_CN","GET", null);
            System.out.println(jsonObject);
            String redirect_url="http://ab44e8e9.ngrok.io/user/index";
            String url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx9893470477aed97e&redirect_uri="+ URLEncoder.encode(redirect_url,"UTF-8")+"&response_type=code&scope=snsapi_userinfo&state=123456#wechat_redirect";
            System.out.println(url);
            // 回复文本消息
            TextMessage textMessage = new TextMessage();
            textMessage.setToUserName(fromUserName);  
            textMessage.setFromUserName(toUserName);  
            textMessage.setCreateTime(new Date().getTime());  
            textMessage.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_TEXT);  
            textMessage.setFuncFlag(0);
  
            // 文本消息  
            if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_TEXT)) {  
                respContent = "您发送的是文本消息！";  
            }  
            // 图片消息  
            else if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_IMAGE)) {  
                respContent = "您发送的是图片消息！";  
            }  
            // 地理位置消息  
            else if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_LOCATION)) {  
                respContent = "您发送的是地理位置消息！";  
            }  
            // 链接消息  
            else if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_LINK)) {  
                respContent = "您发送的是链接消息！";  
            }  
            // 音频消息  
            else if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_VOICE)) {  
                respContent = "您发送的是音频消息！";  
            }  
            // 事件推送  
            else if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_EVENT)) {  
                // 事件类型  
                String eventType = requestMap.get("Event");  
                // 订阅  
                if (eventType.equals(MessageUtil.EVENT_TYPE_SUBSCRIBE)) {  
                    respContent = "谢谢您的关注！";  
                }  
                // 取消订阅  
                else if (eventType.equals(MessageUtil.EVENT_TYPE_UNSUBSCRIBE)) {  
                    // 取消订阅后用户再收不到公众号发送的消息，因此不需要回复消息  
                }  
                // 自定义菜单点击事件  
                else if (eventType.equals(MessageUtil.EVENT_TYPE_CLICK)) {  
                	//事件KEY值，与创建自定义菜单时指定的KEY值对应  
                	String eventKey = requestMap.get("EventKey");
                	respContent = "点击菜单："+eventKey; 
                }  
            }  
  
            textMessage.setContent(respContent);  
            respMessage = MessageUtil.textMessageToXml(textMessage);  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
  
        return respMessage;  
    }  

}
